import LGXCom from '../../common/common.js'
import rotategraphsvc from "../../service/rotategraphsvc.js";

import globalConst from "../../common/globalConst.js";
var global = globalConst.GlobalConst;

var DragRotateSvc = {}

DragRotateSvc.dragRotate = function (controlGraph, graphList, rotCntPt, dragPt, offsetPt, mapscale) {
    let succes = false;
    //拖动前旋转特征杆方向向量，世界坐标系
    let preRotHandVector = new LGXCom.LGXVector();
    preRotHandVector.setBy2Pt(rotCntPt, dragPt);

    //拖动前旋转特征杆方向向量，世界坐标系
    let nextRotHandVector = new LGXCom.LGXVector();
    let x = dragPt.x + offsetPt.x;
    let y = dragPt.y + offsetPt.y;
    let nextDragPt = { x, y };
    nextRotHandVector.setBy2Pt(rotCntPt, nextDragPt);

    //旋转角度，逆时针为正
    let rotAngleDgree = preRotHandVector.angleByCrossMultiply2Another(nextRotHandVector);
    let validRotDegree = this.getValidRotDegree(controlGraph, rotAngleDgree);
    if (Math.abs(validRotDegree) > global.ZERO_4E) {
        rotategraphsvc.RotateGraphSvc.rotate(rotCntPt, validRotDegree, graphList, mapscale);
        succes = true;
    }
    graphList.forEach(p => {
        p.setModifing(1);
    });
    controlGraph.setModifing(1);
    return succes;
}

DragRotateSvc.getValidRotDegree = function (controlGraph, rotAngleDgree) {
    let ctrlGraphNewAngle = controlGraph.getAngle() + rotAngleDgree;
    const degreeGap = 5;
    let validRotDegree = rotAngleDgree;
    if (controlGraph.getModifing() == 1) {
        if (Math.abs(ctrlGraphNewAngle) < degreeGap) {
            validRotDegree = 0;
        }
        else if (Math.abs(ctrlGraphNewAngle - 90) < degreeGap) {
            validRotDegree = 90 - controlGraph.getAngle();
        }
        else if (Math.abs(ctrlGraphNewAngle - 180) < degreeGap) {
            validRotDegree = 180 - controlGraph.getAngle();
        }
        else if (Math.abs(ctrlGraphNewAngle - 270) < degreeGap) {
            validRotDegree = 270 - controlGraph.getAngle();
        }
        else if (Math.abs(ctrlGraphNewAngle - 360) < degreeGap) {
            validRotDegree = - controlGraph.getAngle();
        }
    }
    return validRotDegree;
}

export default { DragRotateSvc }